package gui;

import houseHold.House;
import houseHold.HouseHoldObject;
import houseHold.Room;
import houseHold.RoomType;
import houseHold.electricObjects.ElectricObject;
import houseHold.waterObjects.WaterObject;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Point;
import java.awt.Rectangle;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;

import sensor.Sensor;
import sensor.SensorType;
import RoomPanel.Aproach;
import constants.Constants;

public class RoomInformationDialog extends JDialog {

	private static final long serialVersionUID = 1L;

	public static void main(String args[]) {
		Room callingRoom = new Room();
		callingRoom.setType(RoomType.HALLWAY);
		Room c2 = new Room();
		c2.setName("r2");
		HouseHoldObject callingObject = new ElectricObject();
		HouseHoldObject callingObject2 = new WaterObject();
		callingObject.setLocation(callingRoom);
		callingObject2.setLocation(callingRoom);
		callingRoom.addItem(callingObject);
		callingRoom.addItem(callingObject2);
		House.getInstance().addRoom(callingRoom);
		House.getInstance().addRoom(c2);
		Sensor s1, s2, s3;
		s1 = new Sensor();
		s1.setType(SensorType.ELECTRIC_SENSOR);
		s2 = new Sensor();
		s2.setType(SensorType.WATER_SENSOR);
		s3 = new Sensor();
		s3.setType(SensorType.GAS_SENSOR);
		callingObject.setSensor(s1);
		callingObject2.setSensor(s2);
		callingRoom.addSensor(s1);
		callingRoom.addSensor(s2);
		RoomInformationDialog dd = new RoomInformationDialog(new JFrame());
		dd.initialize(callingRoom);
		dd.show();
	}

	private JButton AddItemButton = null;
	private JButton AddSensorButton = null;
	private Room callingRoom = null; // @jve:decl-index=0:
	private JLabel ErrorLabel = null;
	private JButton Exit = null;
	private JPanel InfoPanel = null;
	private JPanel jContentPane = null;
	private JScrollPane jScrollPane = null;
	private JScrollPane jScrollPane1 = null;
	private JList ObjectList = null;
	private JPanel ObjectListBorderPanel = null;
	private Frame owner;// @jve:decl-index=0:
	private JTextField RoomHeight = null;
	private JLabel RoomHeightLabel = null;
	private JTextField RoomLength = null;
	private JLabel RoomLengthLabel = null;
	private JTextField RoomName = null;
	private JLabel RoomNameLabel = null;
	private JLabel roomTypeLabel = null;
	private JTextField RoomWidth = null;
	private JLabel RoomWidthLabel = null;

	private JButton Save = null;

	private JPanel SensorBorderPanel = null;

	private JList SensorList = null;

	private JComboBox Type = null;

	/**
	 * @param owner
	 */
	public RoomInformationDialog(Frame owner) {
		super(owner);
		this.owner = owner;
	}

	/**
	 * This method initializes AddItemButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getAddItemButton() {
		if (AddItemButton == null) {
			AddItemButton = new JButton();
			AddItemButton.setText("Add new Object");
			AddItemButton
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							final AddObject AO = new AddObject(HouseEditor
									.getInstance(), getDialog());
							AO.initialize(callingRoom);
							AO.setSize(545, 275);
							Point loc = HouseEditor.getInstance().getLocation();
							loc.translate(50, 50);
							AO.setLocation(loc);
							AO.setVisible(true);
							getDialog().setEnabled(false);
						}
					});
		}
		return AddItemButton;
	}

	/**
	 * This method initializes AddSensorButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getAddSensorButton() {
		if (AddSensorButton == null) {
			AddSensorButton = new JButton();
			AddSensorButton.setText("Add new Sensor");
			AddSensorButton
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							final AddSensor AD = new AddSensor(HouseEditor
									.getInstance(), getDialog());
							AD.initialize(callingRoom);
							AD.setSize(290, 280);
							Point loc = HouseEditor.getInstance().getLocation();
							loc.translate(50, 50);
							AD.setLocation(loc);
							AD.setVisible(true);
							getDialog().setEnabled(false);
						}
					});
		}
		return AddSensorButton;
	}

	private RoomInformationDialog getDialog() {
		return this;
	}

	/**
	 * This method initializes Exit
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getExit() {
		if (Exit == null) {
			Exit = new JButton();
			Exit.setBounds(new Rectangle(150, 225, 100, 25));
			Exit.setToolTipText("Exit room information dialog.");
			Exit.setText("Exit");
			Exit.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					owner.setEnabled(true);
					getDialog().setVisible(false);

				}
			});
		}
		return Exit;
	}

	/**
	 * This method initializes InfoPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getInfoPanel() {
		if (InfoPanel == null) {
			ErrorLabel = new JLabel();
			ErrorLabel.setBounds(new Rectangle(25, 180, 195, 25));
			ErrorLabel.setFont(new Font("SansSerif", Font.BOLD, 12));
			ErrorLabel.setForeground(new Color(196, 0, 0));
			ErrorLabel.setHorizontalAlignment(SwingConstants.CENTER);
			ErrorLabel.setText("");
			RoomLengthLabel = new JLabel();
			RoomLengthLabel.setBounds(new Rectangle(20, 150, 60, 25));
			RoomLengthLabel.setHorizontalAlignment(SwingConstants.CENTER);
			RoomLengthLabel.setFont(new Font("SansSerif", Font.BOLD, 12));
			RoomLengthLabel.setToolTipText("Room length in centimeters");
			RoomLengthLabel.setText("Height:");
			RoomHeightLabel = new JLabel();
			RoomHeightLabel.setBounds(new Rectangle(20, 120, 60, 25));
			RoomHeightLabel.setHorizontalAlignment(SwingConstants.CENTER);
			RoomHeightLabel.setFont(new Font("SansSerif", Font.BOLD, 12));
			RoomHeightLabel.setToolTipText("Room width  in cenitmeters");
			RoomHeightLabel.setText("Width:");
			RoomWidthLabel = new JLabel();
			RoomWidthLabel.setBounds(new Rectangle(20, 90, 60, 25));
			RoomWidthLabel.setHorizontalAlignment(SwingConstants.CENTER);
			RoomWidthLabel.setFont(new Font("SansSerif", Font.BOLD, 12));
			RoomWidthLabel.setToolTipText("Room heigth in cenitmeters");
			RoomWidthLabel.setText("Length:");
			RoomNameLabel = new JLabel();
			RoomNameLabel.setBounds(new Rectangle(20, 60, 60, 25));
			RoomNameLabel.setHorizontalAlignment(SwingConstants.CENTER);
			RoomNameLabel.setFont(new Font("SansSerif", Font.BOLD, 12));
			RoomNameLabel
					.setToolTipText("Generated automatically from RoomID & RoomType");
			RoomNameLabel.setText("Name:");
			roomTypeLabel = new JLabel();
			roomTypeLabel.setBounds(new Rectangle(20, 25, 50, 25));
			roomTypeLabel.setHorizontalAlignment(SwingConstants.CENTER);
			roomTypeLabel.setFont(new Font("SansSerif", Font.BOLD, 12));
			roomTypeLabel.setToolTipText("Room Type");
			roomTypeLabel.setText("Type:");
			InfoPanel = new JPanel();
			InfoPanel.setBounds(new Rectangle(15, 8, 240, 212));
			InfoPanel.setBackground(new Color(250, 250, 250));
			InfoPanel.setLayout(null);
			InfoPanel.add(getRoomName(), null);
			InfoPanel.add(getType(), null);
			InfoPanel.add(getRoomWidth(), null);
			InfoPanel.add(getRoomHeight(), null);
			InfoPanel.add(getRoomLength(), null);
			InfoPanel.setBorder(BorderFactory.createCompoundBorder(
					BorderFactory.createBevelBorder(BevelBorder.RAISED),
					BorderFactory
							.createTitledBorder(BorderFactory.createLineBorder(
									Color.gray, 5), "Room Inormation",
									TitledBorder.DEFAULT_JUSTIFICATION,
									TitledBorder.DEFAULT_POSITION, new Font(
											"SansSerif", Font.BOLD, 14),
									Constants.blue)));
			InfoPanel.add(roomTypeLabel, null);
			InfoPanel.add(RoomNameLabel, null);
			InfoPanel.add(RoomWidthLabel, null);
			InfoPanel.add(RoomHeightLabel, null);
			InfoPanel.add(RoomLengthLabel, null);
			InfoPanel.add(ErrorLabel, null);
		}
		return InfoPanel;
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setBackground(new Color(250, 250, 250));
			jContentPane.setLayout(null);
			jContentPane.add(getInfoPanel(), null);
			jContentPane.add(getSave(), null);
			jContentPane.add(getExit(), null);
			jContentPane.add(getObjectListBorderPanel(), null);
			jContentPane.add(getSensorBorderPanel(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getSensorList());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jScrollPane1
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane1() {
		if (jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane();
			jScrollPane1.setViewportView(getObjectList());
		}
		return jScrollPane1;
	}

	/**
	 * This method initializes ObjectList
	 * 
	 * @return javax.swing.JList
	 */
	private JList getObjectList() {
		if (ObjectList == null) {
			ObjectList = new JList();
			ObjectList.setFixedCellHeight(16);
			ObjectList.setToolTipText("Right-Click to remove an item");
			ObjectList.addMouseListener(new java.awt.event.MouseAdapter() {
				@Override
				public void mousePressed(java.awt.event.MouseEvent e) {
					if (e.getButton() > 1 && !ObjectList.isSelectionEmpty()) {

						ObjectListPopupMenu OLPM = new ObjectListPopupMenu(
								ObjectList.getSelectedValue().toString(),
								callingRoom.getName(), 0);
						OLPM.getRoomPopUp(getDialog()).show(ObjectList,
								e.getX(), e.getY());
					}
				}
			});
		}
		return ObjectList;
	}

	/**
	 * This method initializes ObjectListBorderPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getObjectListBorderPanel() {
		if (ObjectListBorderPanel == null) {
			ObjectListBorderPanel = new JPanel();
			ObjectListBorderPanel.setBackground(new Color(250, 250, 250));
			ObjectListBorderPanel.setLayout(new BorderLayout());
			ObjectListBorderPanel.setBounds(new Rectangle(285, 10, 180, 250));
			ObjectListBorderPanel.setBorder(BorderFactory.createCompoundBorder(
					BorderFactory.createBevelBorder(BevelBorder.RAISED),
					BorderFactory
							.createTitledBorder(BorderFactory.createLineBorder(
									Color.gray, 5), "Object List",
									TitledBorder.DEFAULT_JUSTIFICATION,
									TitledBorder.DEFAULT_POSITION, new Font(
											"SansSerif", Font.BOLD, 14),
									Constants.blue)));
			ObjectListBorderPanel.add(getJScrollPane1(), BorderLayout.CENTER);
			ObjectListBorderPanel.add(getAddItemButton(), BorderLayout.SOUTH);
		}
		return ObjectListBorderPanel;
	}

	/**
	 * This method initializes RoomHeight
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getRoomHeight() {
		if (RoomHeight == null) {
			RoomHeight = new JTextField();
			RoomHeight.setBounds(new Rectangle(100, 150, 120, 25));
			RoomHeight.setFont(new Font("SansSerif", Font.BOLD, 12));
			RoomHeight.setHorizontalAlignment(SwingConstants.CENTER);
			RoomHeight.addKeyListener(new java.awt.event.KeyAdapter() {
				@Override
				public void keyReleased(java.awt.event.KeyEvent e) {
					try {
						Integer.parseInt(RoomHeight.getText());
						writeError("");
					} catch (Exception ee) {
						writeError("Invalid Height");
					}
				}

			});
		}
		return RoomHeight;
	}

	/**
	 * This method initializes RoomLength
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getRoomLength() {
		if (RoomLength == null) {
			RoomLength = new JTextField();
			RoomLength.setBounds(new Rectangle(100, 90, 120, 25));
			RoomLength.setFont(new Font("SansSerif", Font.BOLD, 12));
			RoomLength.setHorizontalAlignment(SwingConstants.CENTER);
			RoomLength.addKeyListener(new java.awt.event.KeyAdapter() {
				@Override
				public void keyReleased(java.awt.event.KeyEvent e) {
					try {
						Integer.parseInt(RoomLength.getText());
						writeError("");
					} catch (Exception ee) {
						writeError("Invalid length");
					}
				}

			});
		}
		return RoomLength;
	}

	/**
	 * This method initializes RoomName
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getRoomName() {
		if (RoomName == null) {
			RoomName = new JTextField();
			RoomName.setBounds(new Rectangle(100, 60, 120, 25));
			RoomName.setFont(new Font("SansSerif", Font.BOLD, 12));
			RoomName.setEditable(false);
			RoomName.setBackground(Color.white);
			RoomName.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return RoomName;
	}

	/**
	 * This method initializes RoomWidth
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getRoomWidth() {
		if (RoomWidth == null) {
			RoomWidth = new JTextField();
			RoomWidth.setBounds(new Rectangle(100, 120, 120, 25));
			RoomWidth.setFont(new Font("SansSerif", Font.BOLD, 12));
			RoomWidth.setHorizontalAlignment(SwingConstants.CENTER);
			RoomWidth.addKeyListener(new java.awt.event.KeyAdapter() {
				@Override
				public void keyReleased(java.awt.event.KeyEvent e) {
					try {
						Integer.parseInt(RoomWidth.getText());
						writeError("");
					} catch (Exception ee) {
						writeError("Invalid width");
					}
				}

			});
		}
		return RoomWidth;
	}

	private JButton getSave() {
		if (Save == null) {
			Save = new JButton();
			Save.setBounds(new Rectangle(20, 225, 100, 25));
			Save.setToolTipText("Save Changes");
			Save.setText("Update");
			Save.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					callingRoom.setLength(Integer
							.parseInt(RoomLength.getText()));
					callingRoom.setWidth(Integer.parseInt(RoomWidth.getText()));
					callingRoom.setHeight(Integer
							.parseInt(RoomHeight.getText()));
					callingRoom.setName(RoomName.getText());
					callingRoom.setType((RoomType) Type.getSelectedItem());
					if (HouseEditor.getInstance().getPanel(callingRoom.getID()) != null) {
						HouseEditor.getInstance().getPanel(callingRoom.getID())
								.setSize(callingRoom.getWidth() / 5,
										callingRoom.getLength() / 5);
						HouseEditor.getInstance().RoomBuilder.repaint();
						HouseEditor.getInstance().getPanel(callingRoom.getID())
								.setToolTipText(callingRoom.getName());
						Aproach.computeCount(HouseEditor.panels);
					}
					System.out.println(callingRoom);
				}
			});
		}
		return Save;
	}

	/**
	 * This method initializes SensorBorderPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getSensorBorderPanel() {
		if (SensorBorderPanel == null) {
			SensorBorderPanel = new JPanel();
			SensorBorderPanel.setBackground(new Color(250, 250, 250));
			SensorBorderPanel.setLayout(new BorderLayout());
			SensorBorderPanel.setBounds(new Rectangle(480, 10, 180, 250));
			SensorBorderPanel.setBorder(BorderFactory.createCompoundBorder(
					BorderFactory.createBevelBorder(BevelBorder.RAISED),
					BorderFactory
							.createTitledBorder(BorderFactory.createLineBorder(
									Color.gray, 5), "Sensor List",
									TitledBorder.DEFAULT_JUSTIFICATION,
									TitledBorder.DEFAULT_POSITION, new Font(
											"SansSerif", Font.BOLD, 14),
									Constants.blue)));
			SensorBorderPanel.add(getJScrollPane(), BorderLayout.CENTER);
			SensorBorderPanel.add(getAddSensorButton(), BorderLayout.SOUTH);

		}
		return SensorBorderPanel;
	}

	/**
	 * This method initializes SensorList
	 * 
	 * @return javax.swing.JList
	 */
	private JList getSensorList() {
		if (SensorList == null) {
			SensorList = new JList();
			SensorList.setFixedCellHeight(16);
			SensorList.setToolTipText("Right-Click to remove an sensor");
			SensorList.addMouseListener(new java.awt.event.MouseAdapter() {
				@Override
				public void mousePressed(java.awt.event.MouseEvent e) {
					if (e.getButton() > 1 && !SensorList.isSelectionEmpty()) {
						ObjectListPopupMenu OLPM = new ObjectListPopupMenu(
								SensorList.getSelectedValue().toString(),
								callingRoom.getName(), 1);
						OLPM.getRoomPopUp(getDialog()).show(SensorList,
								e.getX(), e.getY());

					}
				}
			});
		}
		return SensorList;
	}

	/**
	 * This method initializes Type
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getType() {
		if (Type == null) {
			Type = new JComboBox(houseHold.RoomType.values());
			Type.setBounds(new Rectangle(80, 25, 140, 25));
			Type.setBackground(new Color(250, 250, 250));
			Type.setFont(new Font("SansSerif", Font.BOLD, 12));
			Type.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					RoomName.setText(Type.getSelectedItem() + "#"
							+ callingRoom.getID());
				}
			});
		}
		return Type;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	public void initialize(Room cRoom) {
		this.setSize(700, 310);
		this.setContentPane(getJContentPane());
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			@Override
			public void windowClosing(java.awt.event.WindowEvent e) {
				owner.setEnabled(true);
			}
		});
		this.callingRoom = cRoom;
		populateLists();
		populateFields();
	}

	public void populateFields() {
		RoomName.setText(callingRoom.getName());
		RoomLength.setText(callingRoom.getLength() + "");
		RoomWidth.setText(callingRoom.getWidth() + "");
		RoomHeight.setText(callingRoom.getHeight() + "");
		Type.setSelectedItem(callingRoom.getType());
	}

	public void populateLists() {
		ObjectList.setListData(callingRoom.getItemList().values().toArray());
		this.SensorList
				.setListData(callingRoom.getSensors().values().toArray());
	}

	private void writeError(String s) {
		if (s.length() > 0) {
			ErrorLabel.setText(s);
			Save.setEnabled(false);
		} else {
			ErrorLabel.setText("");
			Save.setEnabled(true);
		}
	}
} // @jve:decl-index=0:visual-constraint="10,10"

